package com.liang.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.liang.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000117613898";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDE07nWI3HsgJpmHrl4HEk+cM1JybD8DqMbsVtdXDxDZTmIBWkT0iAdv/19QIal13FnkPsuURtRAF0zJCK6u70qc9bfVfc1TR5t6zXKW2Q6Lzq3p2r17SgN53g0Or/UKseAEFAdR/x01nmk+dj5YxGoN5kMYcrkIyDrI7tzvCJvOZT2Mmy2Tr/cF0uSmWThIecwsY0T/91XZAwSQW4vPDAeQbKbjCl7NlSetCFwfNmGjtpttjhiuDd5wxYEO2qGlCTCOJbxc/LVyfkADrGNpFOsaCfm/AD09umfOnD/Xb5suaoJlyr5O58hhhoLqJ2X+JLZ2kMPjUm4tEX2XDRhexjXAgMBAAECggEBAJooworn5Bi6eTO2qMlcTjmrm/5JDGe/Bx1vggHTdhfLNo5EvpIZp7HlZ6MsYMmPvnDBaxpRyDVfNaASk2jyFi0j4Y23BRIoPscUzGKbxhqUV0b0x5+qig6p1soBobGJeAvjuvvUH/Va7pe6YKijYXcBZMv/5CtfIC9LI0x4c/fsHpcr1Lvk/XLyFs3a19dZovsw1crPTyJ1KrhB+54+WEi0dylbwelgvXYXgl/vpL3oq9L59EeezbszV8X+g5T/RWLrekLNbIxX+z4cR50qvM5Zkc6Q/eEXnM3zbHkn9lr6znlHtdbMZY0OXYWmtPCPF0IbovFboCuVMMvWHKpTqjECgYEA9gD6g8wh210bDZbR4XU6eHm9dV9s+JbCWEB1rge03XILckAjfiDY/1EuDE/3MrdW8OzPmCstg4Wsa52jd+K3pqXagkFPl/cKfxp//k/ftp/uPlauN2D7tH1UdHdUbbT8ZnH6h2B/d5VLsQTZ7EFguu+K16VKANeRZBvvPYEogpMCgYEAzNMxU7cG0AIlBRQKHaOTRkXw/E6EdeKYCoMkYE5/9YEHHymH6eYESP1sGyypQf9mJWSp51vDsQVZbmV8I/otiPE+IERqx4Z/1v1oxJHuvJVOklWg++PPpg7Q0W2NLEaKecjBlGSGv5MSOrlc5hFrKvLN344jjr7554YQt1DBZy0CgYAIIlomN367GSYG2gDQmqa3Sxc3pVMeGSYN8vuxKHITjGzSd0p1JjLmplGGeLJsy+ElED+BAZR8r9PWrxyznxyBdyR1gCFYngOL1l17FDsKd9lR2F3kWOGZwzcirrts8ToTTRuClG9Cjf41uTpXdIX1SGxn2wBKbNZS4BWWDApB3wKBgFClZuqbSedksMbLGSCKY1Zr+3KcR7NSwKKC+DOjNOapIvC2HXmOw2kpvwMEFrCRqtpWRD9/9hEhFhFNjA9uVbdxpRB3X8OnxXYU2Mds3h8KeqFLbdM/42Zytu1+wPMJ40QIM5AF9LhBZBATI+zCNvv2FfFLudxefW8XbkV/WmcRAoGBAIvrlb/uofSqAWPUWaIqDTsfnBxZ4VqLM4q7qkqnYfPZiyuZmopJi0oabb5UIcxHd0P2uQU/LROXt5Q4ul2QF6gsyFr1bCckMY7TNLeMDDB/YT5YQBZi66iZgMgp7ew2/4z7YGnvNOgJ3uz7gse2WhTWU91AyJfS5QK4pjKYjrbH";
            // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAw3CtOVMxrazHHqF4S17pNQdapCdyg3lH7hi7HDjWq4fdH4LsFSMQQpm4jvxVy7FehDJXsgg6UGOryZ2Avs9r5/7HOW4cMI30oEkyfc9avyr9j0wipVlqj16mvfFi0ilY0xHz1bOM509qF5FpJhPksx2QyeNkuFNdBYhjgeudk1oE6Ti0F/V8Zt0rwv1b43dKqQi0aK/csNYRiWTukhV30Y+k5dGnH6XPacsbWHgTBwRyrK8eYBPIQrxdpiMlgSBELQ01F/EBM1AvBp7FryY0l/vj/gznIRnG8kUP/82VZeSZ+mQGrc3mGj1LQAXnk7vWanvncOwJ5b9UWVGbXDAtowIDAQAB" ;
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url="http://gulimall.cn1.utools.club/order/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url;

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\"10m\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        //System.out.println("支付宝的响应："+result);

        return result;

    }
}
